<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Message extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->tologin->isLogin ( site_url ( 'login' ) );
		$this->load->model ( 'message_model' );
		$this->load->model ( 'user_model' );
		$this->load->model('category_model');
		$whereArr = array("status"=>1);
		$this->category_list = $this->category_model->resultAll($whereArr);
	}
	public function index($page=1){
		$data["my_message"] = true;
		$user_message = $this->user_message();
		$user_message['logo'] = ltrim($user_message['logo'],".");
		$data['user_message'] = $user_message;
		$data["category_list"] = $this->category_list;
		$uid = $this->session->userdata('uid');
		$whereArr = "(suid='$uid' AND status=1) OR (ruid='$uid' AND status=1)";
		$message_list = $this->message_model->resultAll($whereArr,"sendtime@DESC");
		$message_list = $this->filter_message_data($message_list);
		foreach($message_list as $key=>$value){
			if($value['ruid'] == $uid){
				$user = $this->find_uid_to_user($value['suid']);
				$message_list[$key]['logo'] = ltrim($user['logo'],".");
				$message_list[$key]['username'] = $user['username'];
			}else{
				$user = $this->find_uid_to_user($value['ruid']);
				$message_list[$key]['logo'] = ltrim($user['logo'],".");
				$message_list[$key]['username'] = $user['username'];
			}
		}
		$count = count($message_list);
		$params = array(
				"url_template" => site_url ( "Message/index/{page}" ),
				"page_count" => $count,
				"page_now" => $page,
				"page_size" => 8,
				"class"=>array("ul"=>"","disabled"=>"am-disabled","active"=>"am-active")
		);
		$params["total_pages"] = ceil ( $params['page_count'] / $params['page_size'] );
		$this->load->library ( 'paging', $params );
		$this->load->library ('ArrayChange');
		$message_list = $this->arraychange->ArrayPage($message_list,$page,4);
		$data['message_list'] = $message_list;
		$this->load->view("my_message",$data);
	}
	/*
	 *显示短信 
	 */
	public function message_content($suid=1){
		$data["my_message"] = true;
		$user_message = $this->user_message();
		$user_message['logo'] = ltrim($user_message['logo'],".");
		$data['user_message'] = $user_message;
		$data["category_list"] = $this->category_list;
		$ruid =$this->session->userdata('uid');
		$whereArr = "(suid = '$suid' AND ruid = '$ruid' AND status = 1) OR (ruid = '$suid' AND suid = '$ruid' AND status =1)";
		$one_message = $this->message_model->rowOrder($whereArr,"sendtime@DESC");
		if(!$one_message['lookStatus']){
			$whereArr1 = array("mid"=>$one_message['mid']);
			$dataArr1 = array("lookStatus"=>1);
			$this->message_model->update($dataArr1,$whereArr1);
		}
		$no_read_count = $this->no_read_message_count();
		$this->session->set_userdata('no_read_count', $no_read_count);
		$message_content_list = $this->message_model->resultCount($whereArr,20,"sendtime@ASC");
		foreach($message_content_list as $key=>$value){
			$suser = $this->find_uid_to_user($value['suid']);
			$message_content_list[$key]['slogo'] = ltrim($suser['logo'],".");
			$message_content_list[$key]['susername'] = $suser['username'];
		}
		$data['message_content_list'] = $message_content_list;
		$data['toUid'] = $suid;
		$this->load->view("message_content",$data);
	}
	/*
	 *回复功能 
	 */
	public function answer($ruid=1){
		$content = $this->input->post("content");
		if(empty($content)){
			$message['flag'] = -1;
			$message = "回复内容不能为空!!";
			$this->show_message($message);
		}
		$dataArr['suid'] = $this->session->userdata('uid');
		$dataArr['ruid'] = $ruid;
		$dataArr['content'] = $content;
		$dataArr['sendtime'] = time();
		$result = $this->message_model->insert($dataArr);
		if($result){
			$message['flag'] = 1;
			$message['content'] = "回复成功!!";
			$this->show_message($message);
		}else{
			$message['flag'] = -1;
			$message['content'] = "回复失败!!";
			$this->show_message($message);
		}
	}
	/*
	 *显示发短信页面 
	 */
	public function send(){
		$data["send_message"] = true;
		$user_message = $this->user_message();
		$user_message['logo'] = ltrim($user_message['logo'],".");
		$data['user_message'] = $user_message;
		$data["category_list"] = $this->category_list;
		$this->load->view("user_send_message",$data);
	}
	/*
	 *搜索发送 
	 */
	public function user_send_message(){
		$rusername = $this->input->post("rusername");
		$content = $this->input->post("content");
		
		$whereArr = array("username"=>$rusername,"status"=>1);
		$user = $this->user_model->row($whereArr);
		
		if(empty($user)){
			$message['flag'] = -1;
			$message['content'] = "无此用户!!";
			$this->show_message($message);
		}
		if(empty($content)){
			$message['flag'] = -1;
			$message['content'] = "私信内容不能为空!!";
		}
		$data['suid'] = $this->session->userdata('uid');
		$data['ruid'] = $user['uid'];
		$data['content'] = $content;
		$data['sendtime'] = time();
		$result = $this->message_model->insert($data);
		if($result){
			$message['flag'] = 1;
			$message['content'] = "发送私信成功!!";
			$this->show_message($message);
		}else{
			$message['flag'] = -1;
			$message['content'] = "发送私信失败!!";
			$this->show_message($message);
		}
	}
	/*
	 *发送短信 
	 */
	public function do_send(){
		$ruid = $this->input->post("ruid");
		$content = $this->input->post("content");
		$suid = $this->session->userdata('uid');
		if(empty($content)){
			$message["flag"] = -1;
			$message["content"] = "私信内容不能为空";
			$this->show_message($message);
		}
		$data['suid'] = $suid;
		$data['ruid'] = $ruid;
		$data['content'] = $content;
		$data['sendtime'] = time();
		$result = $this->message_model->insert($data);
		if($result){
			$message['flag'] = 1;
			$message['content'] = "发送私信成功!!";
			$this->show_message($message);
		}else{
			$message['flag'] = -1;
			$message['content'] = "发送私信失败!!";
			$this->show_message($message);
		}
	}
	/*
	 *查找该用户的为读信息的条数 
	 */
	public function no_read_message_count(){
		$uid = $this->session->userdata('uid');
		$count = 0;
		$whereArr = "(suid='$uid' AND status=1) OR (ruid='$uid' AND status=1)";
		$message_list = $this->message_model->resultAll($whereArr,"sendtime@DESC");
		$message_list = $this->filter_message_data($message_list);
		foreach ($message_list as $key => $value){
			if($value['lookStatus']==0){
				$count++;
			}
		}
		return $count;
	}
	/*
	 *数据的筛除 
	 */
	public function filter_message_data($message_list=array()){
		$uid = $this->session->userdata('uid');
		$new_message_list = array();
		$key_arr = array();
		foreach($message_list as $key => $value){
			if(empty($new_message_list)){
				if(in_array($uid, $value)){
					$new_message_list[] = $value;
				}
				continue;
			}
			if($value['suid']!=$uid){
				$result = $value['suid'];
			}
			if($value['ruid']!=$uid){
				$result = $value['ruid'];
			}
			foreach($new_message_list as $key1 => $value1){
				if($value1['suid']!=$uid){
					$key_arr[] = $value1['suid'];
				}else{
					$key_arr[] = $value1['ruid'];
				}
			}
			if(!in_array($result, $key_arr)){
				$new_message_list[] = $value;
			}
		}
		return $new_message_list;
	}
	/*
	 *查找用户信息
	 */
	public function user_message(){
		$uid=$this->session->userdata('uid');
		$whereArr = array('uid'=>$uid);
		$user = $this->user_model->row($whereArr);
		if($user){
			return $user;
		}
	}
	/*
	 *根据用户id查找用户信息 
	 */
	public function find_uid_to_user($uid){
		$whereArr = array("uid"=>$uid);
		$user = $this->user_model->row($whereArr);
		if($user){
			return $user;
		}
	}
	/*
	 *打印json格式数据 
	 */
	public function show_message($message){
		echo json_encode($message);
		exit;
	}
}